// MyToolBar.cpp : implementation file
//

#include "stdafx.h"
#include "ansieditor.h"
#include "MyToolBar.h"
#include "Registry.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

static const char* TOOLBAR_REG_PATH = "Software\\AnsiEditor\\ToolBar";
/////////////////////////////////////////////////////////////////////////////
// CMyToolBar

CMyToolBar::CMyToolBar(LPCTSTR szBarName) : m_sBarName(szBarName)
{
}

CMyToolBar::~CMyToolBar()
{
}


BEGIN_MESSAGE_MAP(CMyToolBar, CFixMFCToolBar)
	//{{AFX_MSG_MAP(CMyToolBar)
		ON_WM_DESTROY()
	ON_WM_NCPAINT()
	//}}AFX_MSG_MAP
	ON_WM_CONTEXTMENU()
	ON_NOTIFY_REFLECT(TBN_BEGINADJUST, OnToolBarBeginAdjust)
	ON_NOTIFY_REFLECT(TBN_BEGINDRAG, OnToolBarBeginDrag)
	ON_NOTIFY_REFLECT(TBN_CUSTHELP, OnToolBarCustomHelp)
	ON_NOTIFY_REFLECT(TBN_ENDADJUST, OnToolBarEndAdjust)
	ON_NOTIFY_REFLECT(TBN_ENDDRAG, OnToolBarEndDrag)
	ON_NOTIFY_REFLECT(TBN_GETBUTTONINFO, OnToolBarGetButtonInfo)
	ON_NOTIFY_REFLECT(TBN_QUERYDELETE, OnToolBarQueryDelete)
	ON_NOTIFY_REFLECT(TBN_QUERYINSERT, OnToolBarQueryInsert)
	ON_NOTIFY_REFLECT(TBN_RESET, OnToolBarReset)
	ON_NOTIFY_REFLECT(TBN_TOOLBARCHANGE, OnToolBarChange)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyToolBar message handlers

CSize CMyToolBar::CalcDynamicLayout(int nLength, DWORD dwMode)
{
	CSize size = CFixMFCToolBar::CalcDynamicLayout(nLength, dwMode);
	if(dwMode & LM_HORZDOCK)
	{
		size.cy = 33;
		size.cx += 4;
	}
	else if(dwMode & LM_VERTDOCK)
	{
		size.cx = 34;
		size.cy += 4;
	}
	if((dwMode & LM_MRUWIDTH))
	{
		if(size.cx > size.cy)
		{
			size.cx += 4;
		}
	}
	return size;
}

void CMyToolBar::OnContextMenu(CWnd*, CPoint point)
{
//	GetToolBarCtrl().Customize();
}

void CMyToolBar::OnToolBarQueryDelete(NMHDR *notify, LRESULT *result)
{
	*result = TRUE;
}

void CMyToolBar::OnToolBarQueryInsert(NMHDR *notify, LRESULT *result)
{
	*result = TRUE;
}

void CMyToolBar::OnToolBarChange(NMHDR *notify, LRESULT *result)
{
	GetParentFrame()->RecalcLayout();
}

void CMyToolBar::OnToolBarBeginDrag(NMHDR *notify, LRESULT *result)
{
}

void CMyToolBar::OnToolBarEndDrag(NMHDR *notify, LRESULT *result)
{
}

void CMyToolBar::OnToolBarBeginAdjust(NMHDR *notify, LRESULT *result)
{
}

void CMyToolBar::OnToolBarCustomHelp(NMHDR *notify, LRESULT *result)
{
}

void CMyToolBar::OnToolBarEndAdjust(NMHDR *notify, LRESULT *result)
{
	SaveState();
}

void CMyToolBar::OnToolBarGetButtonInfo(NMHDR *notify, LRESULT *result)
{
	TBNOTIFY *tbStruct;
	tbStruct = (TBNOTIFY*)notify;

	if(tbStruct->iItem < garrTbButton.GetSize())
	{
		tbStruct->tbButton = garrTbButton[tbStruct->iItem];
		CString sText;
		sText.LoadString(garrTbButton[tbStruct->iItem].idCommand);
		int nPos = sText.Find('\n');
		char *pc = (char*)(LPCSTR)sText;
		
		lstrcpy(tbStruct->pszText, pc + nPos);
		tbStruct->cchText = sText.GetLength() - nPos;
		*result = TRUE;
	}
	else
	{
		*result = FALSE;
	}
}

void CMyToolBar::OnToolBarReset(NMHDR *notify, LRESULT *result)
{
	GetToolBarCtrl().RestoreState(HKEY_CURRENT_USER, TOOLBAR_REG_PATH,  m_sBarName + " ToolBar Initial Settings");
	GetParentFrame()->RecalcLayout();
}

void CMyToolBar::SaveState()
{
	CString sValueName = m_sBarName + " ToolBar Settings";
	GetToolBarCtrl().SaveState(HKEY_CURRENT_USER, TOOLBAR_REG_PATH, sValueName);
	BYTE *pBuf = NULL;
	DWORD dwSize = 0;
	if(CRegistry::QueryValue(HKEY_CURRENT_USER, TOOLBAR_REG_PATH, sValueName, &pBuf, &dwSize))
	{
		gpApp->WriteProfileBinary("ToolBar", sValueName, pBuf, dwSize);
		delete [] pBuf;
	}
}

void CMyToolBar::RestoreState()
{
	static BOOL bRestored = FALSE;
	CString sValueName;
	if(!bRestored)
	{
		sValueName = m_sBarName + " ToolBar Initial Settings";
		GetToolBarCtrl().SaveState(HKEY_CURRENT_USER, TOOLBAR_REG_PATH, sValueName);
	}
	bRestored = TRUE;

	sValueName = m_sBarName + " ToolBar Settings";
	BYTE *pBuf = NULL;
	DWORD dwSize = 0;
	gpApp->GetProfileBinary("ToolBar", sValueName, &pBuf, (UINT*)&dwSize);
	if(pBuf)
	{
		CRegistry::SetValue(HKEY_CURRENT_USER, TOOLBAR_REG_PATH, sValueName, REG_BINARY, pBuf, dwSize);
		delete [] pBuf;
		GetToolBarCtrl().RestoreState(HKEY_CURRENT_USER, TOOLBAR_REG_PATH, sValueName);
	}
}

void CMyToolBar::OnDestroy() 
{
	SaveState();
	CFixMFCToolBar::OnDestroy();
}

void CMyToolBar::OnNcPaint() 
{
	// get window DC that is clipped to the non-client area
	CWindowDC dc(this);
	CRect rectClient;
	GetClientRect(rectClient);
	CRect rectWindow;
	GetWindowRect(rectWindow);
	ScreenToClient(rectWindow);
	rectClient.OffsetRect(-rectWindow.left, -rectWindow.top);
	dc.ExcludeClipRect(rectClient);

	CRect rcClip;
	dc.GetClipBox(rcClip);
	dc.FillSolidRect(rcClip, GetSysColor(COLOR_BTNFACE));
	// draw borders in non-client area
	rectWindow.OffsetRect(-rectWindow.left, -rectWindow.top);
	DrawBorders(&dc, rectWindow);

	// erase parts not drawn
	dc.IntersectClipRect(rectWindow);
	SendMessage(WM_ERASEBKGND, (WPARAM)dc.m_hDC);

	// draw gripper in non-client area
	DrawGripper(&dc, rectWindow);
	
	// get window DC that is clipped to the non-client area
//	CWindowDC dc(this);
//
//	CRect rectClient;
//	GetClientRect(rectClient);
//	CRect rectWindow;
//	GetWindowRect(rectWindow);
//	ScreenToClient(rectWindow);
//	rectClient.OffsetRect(-rectWindow.left, -rectWindow.top);
////	rectClient.OffsetRect(0, -rectWindow.top);
//	dc.ExcludeClipRect(rectClient);
//
//	CRect rc1;
//	dc.GetClipBox(rc1);
////	dc.FillSolidRect(rc1, GetSysColor(COLOR_BTNFACE));
//	dc.FillSolidRect(rc1, RGB(255, 0, 0));
//
//	return;
//	// draw borders in non-client area
//	rectWindow.OffsetRect(-rectWindow.left, -rectWindow.top);
//	DrawBorders(&dc, rectWindow);
//
//	// erase parts not drawn
//	dc.IntersectClipRect(rectWindow);
//	SendMessage(WM_ERASEBKGND, (WPARAM)dc.m_hDC);
//
//	// draw gripper in non-client area
//	DrawGripper(&dc, rectWindow);
}
